'''

# 购物系统业务需求V1.0

money = 2000
# 钱
commodityList = ['苹果','小米笔记本电脑']
# 商品列表
commodityPriceList = [10,1000]
# 商品价格
# 可以考虑使用字典
shoppingCatList = []
# 购物车列表
consumption = 0
while True:
    sName = input('输入商品名称：')
    if sName in commodityList:
        index = commodityList.index(sName)
        if money >= commodityPriceList[index]:
            money -= commodityPriceList[index]
            consumption += commodityPriceList[index]
            shoppingCatList.append([sName,commodityPriceList[index]])
            print(f'恭喜，购买成功！您的卡余额还剩{money}')
        else:
            print('穷鬼，钱不够！请到其他超市购买！')
    else:
        print('没有这个商品，别瞎弄！')

    _str = input('输入Q，q退出：')
    if _str == 'Q' or _str == 'q':
        break

print('{:10s}{:10s}'.format('商品名称','价格'))
for commodity in shoppingCatList:
    print('{:10s}{:0.2f} x 1'.format(commodity[0],commodity[1]))
print(f'本次消费{consumption}')


'''

# 购物系统业务需求V2.0


import random
import time


def getTime():
    return time.strftime('%H:%M:%S', time.localtime(time.time()))


money = 10000
# 钱
commodityList = ['苹果', '小米笔记本电脑', 'oppo手机', '二手摩托车', '冰箱', '空调', '洗衣机', '旺旺碎冰冰', '玩具枪', '匹克运动鞋']
# 商品列表
coupon = [float(str(random.random())[:3]) for _ in range(10)]
# 优惠券
commodityNum = [int(random.random() * 1000) for _ in range(10)]
# 商品数量
commodityPriceList = [int(random.random() * 500) for _ in range(10)]
# 商品价格
# 可以考虑使用字典
shoppingCatList = []
# 购物车列表
map = {int(random.random() * 10): float(str(random.random())[:3])}
# 随机优惠券
map2 = {}
for e in map:
    map2[e] = map[e]
print('当前优惠券为：', commodityList[list(map2.keys())[0]], '打折为', map2[list(map2.keys())[0]])
consumption = 0
_str = '123'
while True:
    if (_str != 'q' or _str != 'Q') and _str != 'p':
        print('商品信息如下：')
        print('{:10s}{:10s}{:10s}'.format('商品名称', '商品数量', '商品价格'))
        for i in range(len(commodityList)):
            print('{:10s}{:10d}{:9.1f}'.format(commodityList[i], commodityNum[i], commodityPriceList[i]))

        sName = input('输入商品名称：')
        if sName in commodityList:
            index = commodityList.index(sName)
            num1 = (money - commodityPriceList[index] * map.get(index, 1)) // commodityPriceList[index] + 1
            # 最多可购买该产品的数量
            num2 = int(input(f'当前最多可购买{num1}个(只、台)，该产品仓库数量还有{commodityNum[index]}：'))
            #  购物数量
            if num2 > commodityNum[index]:
                print('输入数量超过当前商品的库存了!')
            elif num1 >= num2:
                if num1 >= 1:
                    flag = 1
                    if map.get(index, 1) != 1:
                        flag = map[index]

                    spend = commodityPriceList[index] * map.get(index, 1) + commodityPriceList[index] * (num2 - 1)

                    flag2 = False
                    startIndex = 0
                    for ij in range(len(shoppingCatList)):
                        e = shoppingCatList[ij]
                        if e[0] == sName:
                            flag2 = True
                            startIndex = ij

                    if not flag2:
                        shoppingCatList.append([sName, commodityPriceList[index], num2, spend, getTime(), flag,
                                                int(random.random() * 1000)])
                    else:
                        shoppingCatList[startIndex][2] += num2
                        shoppingCatList[startIndex][3] += spend
                        shoppingCatList[startIndex][4] = getTime()

                    # print(shoppingCatList)
                    commodityNum[index] -= num2

                    if flag != 1:
                        map = {}

                    money -= spend
                    print(f'恭喜，购买成功！您的卡余额还剩{money}')
                else:
                    print('输入的数量不符合要求至少大于0')
            else:
                print('穷鬼，钱不够！请到其他超市购买！')
        else:
            print('没有这个商品，别瞎弄！')
    elif _str == 'p':
        if len(shoppingCatList) == 0:
            print('购物车中没有商品可以退')
        else:
            print('{:10s}{:10s}{:10s}{:10s}'.format('购物编号', '商品名称', '购买数量', '所需金额'))
            for i in range(len(shoppingCatList)):
                shopping = shoppingCatList[i]
                print('{:5d}  {:10s}{:10d}{:9.1f}'.format(shopping[-1], shopping[0], shopping[2], shopping[3]))

            index3 = 0
            while True:
                sName3 = input('请输入商品名称：')
                flagT2 = False
                for e_i in range(len(shoppingCatList)):
                    e2 = shoppingCatList[e_i]
                    if e2[0] == sName3:
                        index3 = e_i
                        flagT2 = True
                        break
                if not flagT2:
                    print('购物车中没有找到这个商品！')
                else:
                    break

            # shopNum = 0
            shop5 = shoppingCatList[index3]
            while True:
                shopNum = int(input('输入需要退的商品数量:'))
                if shopNum > shop5[2]:
                    print(f'输入的数量已经大于购买的商品数量({shop5[2]})')
                elif shopNum <= 0:
                    print('输入的数量需要大于0')
                else:
                    break

            index6 = 0
            for i in range(len(commodityList)):
                if commodityList[i] == sName3:
                    index6 = i
                    break

            commodityNum[index6] += shopNum

            if shopNum == shop5[1]:
                if map2.get(index6,1) != 1:
                    map = {index6:map2[index6]}
                money += shop5[3]
                shop5[2] = shop5[2] + shopNum
                shoppingCatList.pop(index3)
            else:
                money += commodityPriceList[index6] * shopNum
                shop5[2] -= shopNum
                shop5[3] -= commodityPriceList[index6] * shopNum
            print('操作成功！')

    _str = input('输入Q，q退出，输入p进行退货操作：')
    if _str == 'Q' or _str == 'q':
        break

print('{:10s}{:10s}{:10s}{:10s}{:10s}'.format('购物时间','商品名称', '单价','数量','总金额'))
for commodity in shoppingCatList:
    if commodity[5] == 1:
        print('{0:10s}{1:10s}{2:0.2f} {3:10d} {2:0.2f}x{3:d}={4:.1f}'.format(commodity[4],commodity[0], commodity[1],commodity[2],commodity[3]))
    else:
        print('{0:10s}{1:10s}{2:0.2f} {3:10d} {6:d}x{2}+{5:}X{2}={4:.1f}'.format(commodity[4],commodity[0], commodity[1],commodity[2],commodity[3],commodity[5],commodity[2]-1))

    consumption += commodity[3]
print(f'本次消费{consumption}')
